package com.icim.util;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import org.springframework.stereotype.Component;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.CompactWriter;
import com.thoughtworks.xstream.io.xml.DomDriver;

@Component
public class MqOpe {

	public static Object inStr2Obj(String strInMsg, Object inObj) {
		Object objIn = new Object();
		XStream xstream = new XStream(new DomDriver());
		xstream.setMode(XStream.NO_REFERENCES);
		xstream.alias("transaction", inObj.getClass());
		xstream.processAnnotations(inObj.getClass());
		objIn = xstream.fromXML(strInMsg);
		return objIn;
	}

	public static String obj2Str(Object inObj) {
		String relsultXml = null;
		OutputStream output = new ByteArrayOutputStream();
		XStream xstream = new XStream(new DomDriver());
		xstream.alias("transaction", inObj.getClass());
		xstream.autodetectAnnotations(true);
		xstream.setMode(XStream.NO_REFERENCES);
		xstream.marshal(inObj, new CompactWriter(new OutputStreamWriter(output)));
		relsultXml = output.toString().replace("__", "_");// .replace("<id><","<").replace("></id>",">");

		return relsultXml;
	}

}
